//index.js
var t = getApp(),
  a = t.requirejs("core"),
  c = t.requirejs("api/index"),
  e = (t.requirejs("icons"), t.requirejs("wxParse/wxParse"));//对图片进行处理
Page({
  data: {
    official_account:wx.canIUse('official-account'),
    route: "index",
    menu: t.tabBar,
    menu_show:false,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    circular: true,
    icons: t.requirejs("icons"),
    total: 0,
    page: 1,
    show:false,
    display: true,
    hotimg: "/yb_shop/static/images/hotdot.jpg",
    notification: "/yb_shop/static/images/notification.png",
    page_bg_img:'',
    page_bg_color: '',
    showtabbar:false,
    tabbar_index: 0,
    markers: [{
      iconPath: "/yb_shop/static/images/red_position_icon.png",
      title:'地理位置',
      latitude: 34.6284500000,
      longitude: 112.4282100000,
      width: 50,
      height: 50
    }],
    video: false,
    config: t.config,
    page_config:t.page,
    is_copyright:false,//版权不显示
    bookData:{},//清空表单数据用
    //万能表单
    default_pic: '/yb_shop/static/images/add_pic.jpg',
    form: [],
    data: {},
    version: t.version,
    openImg:false//开屏广告
  },
  openImg_show:function(){
      this.setData({
        openImg: false,
      });
      t.setCache('OpenImgShowed', true,60*60*24);
      var showtabbar = this.data.showtabbar;
      this.setData({
        showtabbar: false,
      });
      var that = this;
      setTimeout(function(){
        that.setData({
          showtabbar: showtabbar,
        });
      },30);
  },
  onGotUserInfo: function (q) {
    console.log(q)
    var that = this,
      e = t.getCache("userinfo");
      that.setData({
        display: false
      })
      if (e) {
        return;
      }
      t.getUserInfo(q.detail.userInfo, function (tt) {
        if (tt != 1000) {
          //开屏广告
          if (getApp().page.open_img.imgurl != null && getApp().page.open_img.imgurl != undefined && getApp().page.open_img.imgurl.length > 0) {
            that.setData({
              openImg: true,
            })
          }
        } else {
          that.setData({
            display: true
          })
        }
      }, q.detail.encryptedData, q.detail.iv);
  
  },

  //跳转+收集表单id
  formSubmit: function (k) {
    var vv = { currentTarget: { dataset: {} } },
      data = k.detail.value;
    vv.currentTarget.dataset = data;
    a.menu_url(vv, 1);
    var d = {}
    d.formid = k.detail.formId;
    d.openid = getApp().getCache("userinfo").openid;
    d.username = getApp().getCache("userinfo").nickName;
    a.get('Market/getFormid', d, function (t) {
      console.log(t)
    })

  },

  //跳转
  to_url:function(k){
    a.menu_url(k, 1);
  },

  //底部导航跳转
  menu_url: function (k) {
    a.menu_url(k,2);
  },

  onLoad: function (options) {
    a.compatible_phonex(this);
    var that=this;
    getApp().getExtC(function () {
      if (options && options.pid) {
        var name = t.get_user_name(options.pid, that);
        that.setData({
          pid: options.pid
        })
        t.set_share_pid(options.pid);
        t.setCache('pid', options.pid);
      }
    getApp().get_menu(function (x) { 
      
      that.setData({
        menu: getApp().tabBar,
        page_config:getApp().page
      })

      wx.setNavigationBarTitle({
        title: getApp().page.name ? decodeURIComponent(getApp().page.name) : '首页'
      })

      if (wx.setBackgroundColor) {
        wx.setBackgroundColor({
          backgroundColor: getApp().page.bg_color ? getApp().page.bg_color : '#f2f3f4'
        })
      }
  
      that.setData({
        page_bg_img: getApp().page.bg_img ? getApp().page.bg_img : '',
        page_bg_color: getApp().page.bg_color ? getApp().page.bg_color : '',
      })
      
      c.getArea();
      that.getList();
      a.setting();

      var e = t.getCache("userinfo");
      if (e) {
        that.setData({
          display: false
        })
      }
    
      var showopenimg = t.getCache('OpenImgShowed');
      var showopenimgtype = typeof showopenimg;
      if (!that.data.display && (showopenimgtype == "string" || showopenimgtype == "undefined"))
      {
        if (getApp().page.open_img.imgurl != null && getApp().page.open_img.imgurl != undefined && getApp().page.open_img.imgurl.length > 0)
        {
          that.setData({
            openImg: true
          });
        }
      }
      else
      {
        that.setData({
          openImg: false
        });
      }

    })
  })  //第三方平台
  },
  //留言提交
  formBook:function(e){
    var that=this,
    data=e.detail.value;
    if (data.name==''){
      a.error('姓名不能为空');
      return;
    }else if(data.phone.length!=11){
      a.error('手机号格式不正确');
      return;
    }
    // else if (data.booktime == '') {
    //   a.error('预约时间不能为空');
    //   return;
    // }
    data.user_id = getApp().getCache("userinfo").uid;
    a.get('Index/WriteBook',data,function(t){
      if(t.code==0){
        that.setData({
          bookData:{}
        });
        a.success('提交成功');
      }else{
        a.alert(t.msg);
      }
    })
   
  },
  /**
   * 监听开始时间
   */
  listenerTime: function (e) {
    //调用setData()重新绘制
    this.setData({
      "bookData.book_time": e.detail.value,
    });
  },
  
  onShow: function () {
  },

 getList:function(){
   var that = this;
   c.indexMod(that, function (t) {
             that.setData(t);
           })
    },
  /**
 * 下拉刷新
 */
  onPullDownRefresh: function () {
    this.onLoad();
    wx.stopPullDownRefresh();
  },
  /**
   * 地图
   */
  navigate: function (e) {
    var t = a.pdata(e);
    if (t.name && t.lat && t.lng) {
      a.tx_map(t.lat, t.lng, t.name)
    } else {
      a.toast('不能获取到该位置')
    }
  },
  phone: function (t) {
    a.phone(t)
  },
  
 // 万能表单提交begin//
  formPower: function (e) {
    var form_id = e.detail.formId;
    c.formSubmit(this, e, form_id);
  },
  bindPickerChange: function (e) {
    c.bindPickerChange(this, e);
  },
  listen_time_two: function (e) {
    c.listen_time_two(this, e);
  },
  chooseImageTap1: function (e) {
    var id = a.pdata(e).id;
    c.upload(this, id, 'form_pic', 1)
  },
  chooseImageTap2: function (e) {
    var id = a.pdata(e).id;
    c.upload(this, id, 'form_pic', 0)
  },
  Image_del: function (e) {
    c.Image_del(this, e);
  },
   // 万能表单提交end//
  /**
   * 图片预览
   */
  previewImage:function(e){
   var url=a.pdata(e).url,
   arr=a.pdata(e).arr;
   a.previewImage(url, arr,'imgurl')
  },
 
 //长按版权展示
  copyright:function(){
    var that=this,
      i = that.data.is_copyright;
      that.setData({
        is_copyright:i?false:true
      })
    
  },
  /**
      * 用户点击右上角分享
      */
  onShareAppMessage: function (e) {
    var that = this.data,
      uid = getApp().getCache('userinfo').uid ? getApp().getCache('userinfo').uid : 0;
    return {
     // title: that.name,
      path: '/yb_shop/pages/index/index?pid=' + uid,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }

  },

  official_account_load: function(e)
  {
    console.log("official_account_load");
    console.log(e);
  },

  official_account_err: function (e) {
    console.log("official_account_err");
    console.log(e);
  },




})
